package com.xxchat.controller;

import com.xxchat.annotation.GlobalInterceptor;
import com.xxchat.entity.constants.SysConstants;
import com.xxchat.entity.dto.SysSettingDTO;
import com.xxchat.entity.vo.ResponseVO;
import com.xxchat.redis.RedisComponent;
import com.xxchat.utils.FileUploadUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController("adminSettingController")
@RequestMapping("/admin")
public class AdminSettingController extends ABaseController {

    @Resource
    private RedisComponent redisComponent;

    @Resource
    private FileUploadUtils fileUploadUtils;

    @RequestMapping("/getSysSetting")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO getSysSetting() {
        return getSuccessResponseVO(redisComponent.getSysSetting());
    }

    @RequestMapping("/saveSysSetting")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO saveSysSetting(@RequestPart SysSettingDTO sysSettingDTO,
                                     @RequestPart MultipartFile robotFile,
                                     @RequestPart MultipartFile robotCover) {
        if (null != robotFile) {
            String fileName = SysConstants.ROBOT_UID + SysConstants.IMAGE_SUFFIX;
            this.fileUploadUtils.handleAvatarFile(fileName, robotFile);
        }
        if (null != robotCover) {
            String fileName = SysConstants.ROBOT_UID + SysConstants.IMAGE_COVER_SUFFIX;
            this.fileUploadUtils.handleAvatarFile(fileName, robotCover);
        }
        redisComponent.saveSysSetting(sysSettingDTO);
        return getSuccessResponseVO(null);
    }
}
